<?php
/**
 * This package collect all the Engine tools and it is its entry point.
 *
 * <p>This package is to be included each time you wan to start the engine like this:
 * <code>
 * require_once "Engine/engine.php";
 * </code></p>
* <p><b>How does it work?</b> T
*</p>
 * <p>Also, the following variable can be defined to control the presence of HTML tags in error reporting:
 * <code>$error_reporting_style = "html" | "text";</code> A velue of "html" will allow for HTML tags in error messages.</p>
* @package EngineStartup
* @author Willy Gardiol <willy@gardiol.org>
* @version 1.0
*@todo Implement statistic management (per-atom hits, o per book hits?)
*@todo Implement a more sophisticated access control
*@todo Fix the management console
 */

/**
* PHP version checking. Ensure we have a supported version of PHP ( >= 5.2.0 )
*
* Since this engine relies on classes and other features that are only available from
* PHP version 5.2.0 onward, this function checks it immediately BEFORE the engine inizialization
*/
function check_version()
{
	$version = PHP_VERSION;
	if ( !( ($version{0} > 4) || (($version{0} == 5)&&($version{2} >= 2))) )
	{
		echo "At least PHP 5.2.0 is required, sorry, this engine will not work on your server. Please upgrade.";
		die();
	}
}
check_version();

/**
* Define whether errors should be reported as HTML or plain text
* The global string $error_reporting_style Can be either "html" or "text": it is used to format error reporting.
* Check <i>Core/report_error.php</i> for further details.
*/
if ( !isset($error_reporting_style) )
	$error_reporting_style = "html";

define("BASEPATH", dirname(__file__).DIRECTORY_SEPARATOR );

set_include_path( BASEPATH."CoreClasses".DIRECTORY_SEPARATOR);
spl_autoload_extensions(".class.php");
spl_autoload_register();
spl_autoload_register("autoload_plugins");

$profile_init = new CoreProfile("Initialize Core");
Core::Initialize();
require_once "ErrorReporting.php";
$profile_init->Stop();
unset( $profile_init );

# Ensure default folders exists
if ( !file_exists( "site_data" ) )
	mkdir( "site_data" );

/**
* This function is the main entry point to the I18N. You MUST use this.
*@param string $str The string to translate
*@return string the translated string
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*@package Core
*@todo Remove this I18N and use only the smarty plugin instead!
*@subpackage I18N
*/
function I18N( $str )
{
	static $translations = false;
	if ( !$translations )
		$translations = new CoreTranslations();
	return $translations->Translate($str);
}
/**
* Handle autoloading plugins.
*/
function autoload_plugins($classname)
{
	if ( (count( $tmp = explode("_", $classname) ) > 1) &&
		 (($tmp[0] === "Box") || ($tmp[0] === "Brick") ) )
		require_once BASEPATH."plugins".DIRECTORY_SEPARATOR.$tmp[0].DIRECTORY_SEPARATOR.$tmp[1].DIRECTORY_SEPARATOR."$classname.class.php";
}
/**
* Handle the shutdown procedure: fix path and properly shut down/store Atoms.
*/
function ShutdownFunction()
{
	Core::FixCwd();
	AtomCache::Shutdown();
}
register_shutdown_function( "ShutdownFunction" );
?>